package com.angola.sms.extipetrpide

import android.graphics.Paint
import android.graphics.Typeface
import android.text.Spannable
import android.text.SpannableString
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.angola.sms.R

interface OnTextViewLinkClickListener {
    fun onClick(word: String, index: Int)
}

fun TextView.addUnderLineipetrpide() {
    paint.flags = Paint.UNDERLINE_TEXT_FLAG
    paint.isAntiAlias = true
}

/**增加高亮与点击事件*/
fun TextView.addLinkClickModeOnWordsipetrpide(
    words: Array<String>,
    listener: OnTextViewLinkClickListener?
) {
    val spanClick = SpannableString(text)
    for (index in words.withIndex()) {
        val word = index.value
        val wordindex = text.indexOf(word)
        if (wordindex != -1) {
            spanClick.setSpan(
                object : ClickableSpan() {
                    override fun onClick(widget: View) {
                        Log.d("TextView", "onClick: $word")
                        listener?.onClick(word, index.index)
                    }

                    override fun updateDrawState(ds: TextPaint) {
                        super.updateDrawState(ds)
                        ds.color = ContextCompat.getColor(context, R.color.blue)
                        ds.isUnderlineText = false
                        ds.clearShadowLayer()
                    }
                },
                wordindex,
                wordindex + word.length,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }
    movementMethod = LinkMovementMethod.getInstance()
    text = spanClick
    highlightColor = ContextCompat.getColor(context, R.color.transparent)
}

/**增加副标题样式*/
fun TextView.addTipsStyleipetrpide(words: String) {
    val gstart = text.indexOf(words)
    val gend = gstart + words.length
    val spanString = SpannableString(text)
    spanString.setSpan(RelativeSizeSpan(0.8F), gstart, gend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spanString.setSpan(
        ForegroundColorSpan(ContextCompat.getColor(context, R.color.black)),
        gstart,
        gend,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    text = spanString
}

/**增加副标题样式*/
fun TextView.addTipsStyleipetrpide(words: String, sizeSpan: Float, colorSpan: Int) {
    val gstart = text.indexOf(words)
    val gend = gstart + words.length
    val spanString = SpannableString(text)
    spanString.setSpan(RelativeSizeSpan(sizeSpan), gstart, gend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spanString.setSpan(
        ForegroundColorSpan(colorSpan),
        gstart,
        gend,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    text = spanString
}


/**增加副标题样式*/
fun TextView.addBolidStyleipetrpide(words: String, colorSpan: Int) {
    val gstart = text.indexOf(words)
    val gend = gstart + words.length
    val spanString = SpannableString(text)
    spanString.setSpan(StyleSpan(Typeface.BOLD), gstart, gend, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    spanString.setSpan(
        ForegroundColorSpan(colorSpan),
        gstart,
        gend,
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
    )
    text = spanString
}


/**增加高亮样式*/
fun TextView.addHighlightStyle(words: List<String>, color: Int) {
    val spanString = SpannableString(text)

    for (index in 0 until words.size) {
        val edgeString = words[index]
        val edgeFoundList = mutableListOf<Int>()
        val tempString = text.toString().trim()
        var foundIndex = -1
        do {
            val foundIndexTemp = tempString.indexOf(edgeString, foundIndex)
            if (foundIndexTemp != -1) {
                foundIndex = (foundIndexTemp + edgeString.length)
                edgeFoundList.add(foundIndexTemp)
            } else {
                foundIndex = (tempString.length + 1)
            }
        } while (foundIndex <= tempString.length)

        edgeFoundList.forEach {
            val gstart = it
            val gend = it + edgeString.length
            spanString.setSpan(
                ForegroundColorSpan(color),
                gstart,
                gend,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
    }
    text = spanString
}

/**加粗*/
fun TextView.addBoldStyleipetrpide() {
    typeface = Typeface.defaultFromStyle(Typeface.BOLD)
}

/**不加粗*/
fun TextView.addNormalStyleipetrpide() {
    typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
}
